package top.itaso.lib.common;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * @Description: 设备硬件相关信息工具类
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2021/11/22
 */
public class DeviceUtils {
    /**
     * APP能使用的屏幕宽高，也就是扣除了一些系统固定的状态栏啊、虚拟导航栏啊什么的
     *
     * @param activity
     * @return
     */
    public static int[] getScreenSizeParams(Activity activity) {
        Point point = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(point);
        int[] screenParam = new int[2];
        screenParam[0] = point.x;
        screenParam[1] = point.y;
        return screenParam;
    }

    /**
     * 设备的真实屏幕宽高
     *
     * @param activity
     * @return
     */
    public static int[] getScreenRealSizeParams(Activity activity) {
        Point point = new Point();
        activity.getWindowManager().getDefaultDisplay().getRealSize(point);
        int[] screenParam = new int[2];
        screenParam[0] = point.x;
        screenParam[1] = point.y;
        return screenParam;
    }

    /**
     * APP能使用的屏幕宽高，也就是扣除了一些系统固定的状态栏啊、虚拟导航栏啊什么的
     *
     * @param context
     * @return
     */
    public static int[] getScreenParams(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int[] screenParam = new int[2];
        screenParam[0] = displayMetrics.widthPixels;
        screenParam[1] = displayMetrics.heightPixels;
        return screenParam;
    }

    public static int[] getScreenPx(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int[] screenPx = new int[2];
        screenPx[0] = dm.widthPixels;
        screenPx[1] = dm.heightPixels;
        return screenPx;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
